//
//  HMOrderCenterViewController.m
//  HMSupplier
//
//  Created by haohao on 2017/7/3.
//  Copyright © 2017年 haohao. All rights reserved.
//

#import "HMOrderCenterViewController.h"
#import "HomeCell.h"
#import "HMOrderDetailViewController.h"
#import "TodayOrder.h"

@interface HMOrderCenterViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableview;

@property (nonatomic, strong) NSMutableArray * todayOrderArr;

@end

@implementation HMOrderCenterViewController
static NSString * const ID = @"HomeCell";

- (NSMutableArray *)todayOrderArr
{
    if (!_todayOrderArr) {
        _todayOrderArr = [NSMutableArray array];
    }
    return _todayOrderArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
    
    [self setupTableView];
    self.title = @"今日订单";
    [self setupData];
}

- (void)setupData
{
    NSDictionary * param = @{
                             @"uid":[MGAccountTool getUid]
                             };
    [HttpBaseTool get:kTodayOrderUrl params:param success:^(id responseObj) {

        self.todayOrderArr = [TodayOrder mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
    
        [self.tableview reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)setupTableView
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableview = tableView;
    [self.view addSubview:tableView];
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.showsVerticalScrollIndicator = NO;
    [self.tableview registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:ID];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.todayOrderArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.model = self.todayOrderArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width / 2, 40)];
    lable.text = @"菜品信息";
    lable.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:lable];
    
    UILabel * ctext = [[UILabel alloc] initWithFrame:CGRectMake(self.view.width / 2, 0, self.view.width / 2, 40)];
    ctext.text = @"订单总斤数";
    ctext.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:ctext];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HMOrderDetailViewController *vc = [[HMOrderDetailViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.model = self.todayOrderArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
